﻿GdipText(str1, str2:="", x:="", y:="", Font:="Microsoft YaHei UI"){
	static init:=0, Hidefg:=0, DPI:=A_ScreenDPI/96, MonCount:=1, MonLeft, MonTop, MonRight, MonBottom, MonInfo
		, MinLeft:=DllCall("GetSystemMetrics", "Int", 76), MinTop:=DllCall("GetSystemMetrics", "Int", 77)
		, MaxRight:=DllCall("GetSystemMetrics", "Int", 78), MaxBottom:=DllCall("GetSystemMetrics", "Int", 79)
		, xoffset:=9, yoffset:=10, hoffset:=10	; 左边、上边、编码词条间距离增量
	global pToken, @TSF, jichu_for_select_Array, waitspace, Caret, CodeColor
		, TextColor, BackgroundColor, BorderColor, FontSize, TextFont, FontBold
	If (x="Shutdown"){
		If (init){
			init:=0
			Gui, TSF: Destroy
		}
		If (pToken)
			pToken:=Gdip_Shutdown(pToken)
		Return
	} Else If (!init){
		Gui, TSF: -Caption +E0x8080088 +LastFound +hwnd@TSF
		Gui, TSF: Show, NA
		If !pToken&&(!pToken:=Gdip_Startup()){
			MsgBox, 48, GDIPlus Error!, GDIPlus failed to start. Please ensure you have gdiplus on your system, 5
			ExitApp
		}
		SysGet, MonCount, MonitorCount
		SysGet, Mon, Monitor
		init:=1
	}
	If (str1 str2=""){
		; If (!Hidefg){
		; 	Hidefg:=1
		; 	Gui, TSF:Hide
		; }
		hbm := CreateDIBSection(1, 1), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
		UpdateLayeredWindow(@TSF, hdc, 0, 0, 1, 1), SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
		lasth:=0, lastw:=0, MonInfo:=""
		Return
	} Else {
		If (x="")||(y="")
			Return
		If !pToken&&(!pToken:=Gdip_Startup()){
			MsgBox, 48, GDIPlus Error!, GDIPlus failed to start. Please ensure you have gdiplus on your system, 5
			ExitApp
		}
		; If (Hidefg){
		; 	Hidefg:=0
		; 	Gui, TSF:Show, NA w1 h1
		; }
	}
	; 识别扩展屏坐标范围
	x:=(x<MinLeft?MinLeft:x>MaxRight?MaxRight:x), y:=(y<MinTop?MinTop:y>MaxBottom?MaxBottom:y)
	If (MonCount>1&&MonInfo=""){
		If (MonInfo:=MDMF_GetInfo(MDMF_FromPoint(x,y))){
			MonLeft:=MonInfo.Left, MonTop:=MonInfo.Top, MonRight:=MonInfo.Right, MonBottom:=MonInfo.Bottom
		} Else {
			SysGet, Mon, Monitor
		}
	}
	; 计算界面长宽像素
	hbm := CreateDIBSection(1, 1), hdc := CreateCompatibleDC()
	obm := SelectObject(hdc, hbm), G := Gdip_GraphicsFromHDC(hdc)
	CreateRectF(RC, 0, 0, MonRight-MonLeft-20, MonBottom-MonTop)
	hFamily := Gdip_FontFamilyCreate(Font), hFont := Gdip_FontCreate(hFamily, FontSize*DPI, FontBold)
	hFormat := Gdip_StringFormatCreate(0x4000)
	ReturnRC:=StrSplit(Gdip_MeasureString(G, str1 "`n" (str2?str2:" "), hFont, hFormat, RC), "|")
	Gdip_DeleteStringFormat(hFormat), Gdip_DeleteFont(hFont), Gdip_DeleteFontFamily(hFamily)
	SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc), Gdip_DeleteGraphics(G)
	Width:=ReturnRC[3]+xoffset*2, Height:=ReturnRC[4]+yoffset*2+hoffset

	; 画候选界面
	hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC()
	obm := SelectObject(hdc, hbm), G := Gdip_GraphicsFromHDC(hdc)
	Gdip_SetSmoothingMode(G, 2), pBrush := Gdip_BrushCreateSolid("0x" SubStr("FF" BackgroundColor, -7))
	; 背景色
	Gdip_FillRoundedRectangle(G, pBrush, 0, 0, Width-2, Height-2, 5)
	; 编码
	ReturnRC:=StrSplit(Gdip_TextToGraphics(G, str1 (str1?"":" "), "x" xoffset " y" yoffset " c" SubStr("ff" CodeColor, -7) " s" FontSize*DPI (FontBold?" Bold":""), TextFont, Width, Height),"|")
	ReturnRC[2]+=ReturnRC[4]
	; 候选
	Gdip_TextToGraphics(G, str2, "x" ReturnRC[1] " y" ReturnRC[2]+hoffset " c" SubStr("ff" TextColor, -7) " s" FontSize*DPI (FontBold?" Bold":""), Font, Width, Height)
	; 定位提示
	If waitspace&&GetKeyState("Space", "P")
		Gdip_TextToGraphics(G, "   " SubStr("　ａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ",1,StrLen(jichu_for_select_Array[1,2])), "x" ReturnRC[1] " y" ReturnRC[2]+hoffset+FontSize*0.75 " c" SubStr("ff" "ff0000", -7) " s" FontSize*DPI, Font, Width, Height)

	; 边框
	pPen := Gdip_CreatePen("0x" SubStr("FF" BorderColor, -7), 1), Gdip_DrawRoundedRectangle(G, pPen, 0, 0, Width-2, Height-2, 5)
	; 分隔线
	Gdip_DrawLine(G, pPen, 6, ReturnRC[2], Width-9, ReturnRC[2]), Gdip_DeletePen(pPen)
	; 更新界面
	UpdateLayeredWindow(@TSF, hdc, tx:=Min(x, Max(MonLeft, MonRight-Width-2)), ty:=Min(y, Max(MonTop, MonBottom-Height)), Width, Height)
	Gdip_DeleteGraphics(G), SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc), Gdip_DeleteBrush(pBrush)
	WinSet, AlwaysOnTop, On, ahk_id%@TSF%
	If (tx>MonLeft+2)
		Caret.X:=tx
}